﻿using LoginIn.DesignerCS;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace LoginIn
{
    public partial class Form2 : Form
    {
        int flag = 0;
        string ID;
        public Form2(string sname, string Form1_ID)
        {
            InitializeComponent();
            ID = Form1_ID;
            Stu_name.Text = sname;
            Stu_ID.Text = ID;
        }
        private void Hidefun()//实现页面跳转前的所有部件隐藏
        {
            dataGridView_cour.Hide();
            dataGridView_HadPick.Hide();
            Choose_cla.Hide();
            dropcla.Hide();
            paneldit.Hide();

        }
        private void Select_Cla()//实现选课界面的课程展示的数据库搜索
        {
            String whereStmt = "";
            if (Stu_ID.Text.Length > 0)
            {
                whereStmt += "Stu_ID='" + Stu_ID.Text + "'";
            }

            String oldSelectStmt = oleDbDataAdapter2.SelectCommand.CommandText;

            // 将查询条件附加到原查询命令的后面
            String selectStmt = oleDbDataAdapter2.SelectCommand.CommandText;
            if (whereStmt.Length > 0) selectStmt += " WHERE " + whereStmt;

            // 更改oleDbAdapter1的查询命令
            oleDbDataAdapter2.SelectCommand.CommandText = selectStmt;

            // 执行新的查询并填充数据集
            dataSet11.Clear();
            oleDbDataAdapter2.Fill(dataSet11);

            // 恢复oleDbAdapter1的查询命令
            oleDbDataAdapter2.SelectCommand.CommandText = oldSelectStmt;
        }

        private void showcla_fun()
        {
            /*String whereStmt = "Cour_ID NOT IN [SELECT Cour_ID FROM SC WHERE Stu_ID='1120180001']";
            String oldSelectStmt = oleDbDataAdapter1.SelectCommand.CommandText;

            // 将查询条件附加到原查询命令的后面
            String selectStmt = oleDbDataAdapter1.SelectCommand.CommandText;
            if (whereStmt.Length > 0) selectStmt += " WHERE " + whereStmt;

            // 更改oleDbAdapter1的查询命令
            oleDbDataAdapter1.SelectCommand.CommandText = selectStmt;*/

            OleDbCommand command;
            command = new OleDbCommand("SELECT * FROM Cour_Info WHERE Cour_ID NOT IN(SELECT Cour_ID FROM SC WHERE Stu_ID='"+Stu_ID.Text+"')", oleDbConnection1);
            oleDbDataAdapter1.SelectCommand = command;
            // 执行新的查询并填充数据集
            dataSet51.Clear();
            oleDbDataAdapter1.Fill(dataSet51);

            // 恢复oleDbAdapter1的查询命令
            //oleDbDataAdapter1.SelectCommand.CommandText = null;
        }

        private void Statefun(int sta)//实现各个界面的跳转
        {
            if (sta == 1)
            {
                Hidefun();
                moveSidePanel(PersonMeg);

            }
            else if (sta == 2)
            {
                Hidefun();
                moveSidePanel(CourMeg);
            }
            else if (sta == 3)
            {
                Hidefun();
                moveSidePanel(PickMeg);
                //oleDbDataAdapter1.Fill(this.dataSet51);
                showcla_fun();
                Select_Cla();
                dataGridView_cour.Show();
                dataGridView_HadPick.Show();
                Choose_cla.Show();
                dropcla.Show();
            }
            else
            {
                Hidefun();
                moveSidePanel(OtherMeg);
            }
        }//实现sidepanel的界面跳转

        private void Form2_Load(object sender, EventArgs e)
        {

        }

        private void moveSidePanel(Control btn)//实现sidepanel的位置跳转
        {
            sidepanel.Top = btn.Top;
            sidepanel.Height = btn.Height;
        }
        private void pictureBox1_Click(object sender, EventArgs e)//关闭程序
        {
            this.Close();
            Application.Exit();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            Statefun(1);
            paneldit.Show();
            UserControl1 Sinfo = new UserControl1(ID);
            AddControlsToPanel(Sinfo);

        }

        private void AddControlsToPanel(UserControl1 c)
        {
            c.Dock = DockStyle.Fill;
            paneldit.Controls.Clear();
            paneldit.Controls.Add(c);
        }

        private void pictureBox2_Click(object sender, EventArgs e)
        {

        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Statefun(2);
            paneldit.Show();
            UserControl2 Sfo = new UserControl2(ID);
            AddControlsToPanel(Sfo);

        }

        private void AddControlsToPanel(UserControl2 sfo)
        {

            sfo.Dock = DockStyle.Fill;
            paneldit.Controls.Clear();
            paneldit.Controls.Add(sfo);
        }

        private void PickMeg_Click(object sender, EventArgs e)
        {
            Select_Cla();
            Statefun(3);
        }

        private void OtherMeg_Click(object sender, EventArgs e)
        {
            Statefun(4);
        }

        private void pictureBox3_Click(object sender, EventArgs e)
        {

        }

        private void pictureBox3_Click_1(object sender, EventArgs e)
        {
            if (flag == 0)
            {
                dataGridView_cour.Location = new Point(dataGridView_cour.Location.X - 130, dataGridView_cour.Location.Y);
                dataGridView_cour.Width += 130;
                dataGridView_HadPick.Location = new Point(dataGridView_HadPick.Location.X - 130, dataGridView_HadPick.Location.Y);
                dataGridView_HadPick.Width += 130;
                paneldit.Location = new Point(paneldit.Location.X - 130, paneldit.Location.Y);
                paneldit.Width += 130;
                panel1.Width -= 130;
                flag = 1;
                pictureBox3.Location = new Point(30, 5);
                label6.Location = new Point(148, 33);
                label7.Location = new Point(178, 83);
                Stu_name.Location = new Point(278, 37);
                Stu_ID.Location = new Point(306, 83);
            }
            else
            {
                dataGridView_cour.Location = new Point(dataGridView_cour.Location.X + 130, dataGridView_cour.Location.Y);
                dataGridView_cour.Width -= 130;
                dataGridView_HadPick.Location = new Point(dataGridView_HadPick.Location.X + 130, dataGridView_HadPick.Location.Y);
                dataGridView_HadPick.Width -= 130;
                paneldit.Location = new Point(paneldit.Location.X + 130, paneldit.Location.Y);
                paneldit.Width -= 130;
                panel1.Width += 130;
                flag = 0;
                pictureBox3.Location = new Point(165, 5);
                label6.Location = new Point(333, 33);
                label7.Location = new Point(363, 83);
                Stu_name.Location = new Point(463, 37);
                Stu_ID.Location = new Point(491, 83);
            }
        }//实现菜单栏按钮的缩放

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void Choose_cla_Click(object sender, EventArgs e)//实现选课功能
        {
            try
            {
                DataRow row = dataSet141.SC.NewRow();
                row["Cour_ID"] = dataGridView_cour.SelectedCells[0].Value.ToString();
                row["Stu_ID"] = Stu_ID.Text;
                row["Grade"] = 90;
                dataSet141.SC.Rows.InsertAt(row, 0);
                oleDbDataAdapter3.Update(dataSet141, "SC");
                MessageBox.Show("选课成功");
            }
            catch
            {
            }
            finally
            {   
               
                dataSet11.Clear();
                oleDbDataAdapter2.Fill(dataSet11);
                Select_Cla();
                showcla_fun();
            }

        }//实现选课的流程实现

        private void dropcla_Click(object sender, EventArgs e)//实现退课功能
        {
            try
            {
                // 从显示表格中获取客户编号
                int a = dataGridView_HadPick.CurrentRow.Index;
                string nCour_ID = dataGridView_HadPick.Rows[a].Cells[0].Value.ToString();
                Console.WriteLine(nCour_ID);
                // 构造Customer表的删除命令
                String strSCDelete = "DELETE SC WHERE Cour_ID ='" + nCour_ID.ToString() + "'";
                strSCDelete += "AND Stu_ID='" + Stu_ID.Text + "'";
                OleDbCommand cmdSCDelete = new OleDbCommand(strSCDelete, this.oleDbConnection1);
                oleDbConnection1.Open();
                // 删除父表Customer中的记录会自动删除子表CustomerID表中相应的记录
                // 这与所创建的数据模型有关，并不是通用规则。
                cmdSCDelete.ExecuteNonQuery();
                oleDbConnection1.Close();

                MessageBox.Show("退选成功");
            }
            catch
            {
                MessageBox.Show("退选失败");
            }
            // 刷新显示内容

            dataSet11.Clear();
            oleDbDataAdapter2.Fill(dataSet11);
            Select_Cla();
            showcla_fun();
        }

        private void dataGridView_HadPick_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void dataGridView_cour_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

        private void paneldit_Paint(object sender, PaintEventArgs e)
        {

        }
    }
}
